package fm.dongman.modellib.remote;

import android.graphics.Bitmap;

import org.json.JSONObject;

import java.util.HashMap;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.UserEditContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.UserModel;

/**
 * Created by KUIGE on 2017/11/29.
 */

public class UserEditRemoteDataSource implements UserEditContract.IUserEditDataSource {

    private static UserEditRemoteDataSource SINGLETON;

    public static UserEditRemoteDataSource getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new UserEditRemoteDataSource();
        }
        return SINGLETON;
    }

    private UserEditRemoteDataSource()
    {}

    @Override
    public void submitUserInfo(IModel.IUserModel iUserModel, LoadSourceCallback<? super String> loadSourceCallback) {
        UserModel model = (UserModel) iUserModel;
        HashMap<String, String> map = new HashMap<>();
        HashMap<String,String> mapHan = new HashMap<>();
        mapHan.put("x-token", StaticUrl.TOKEN_KEY);
        map.put("name", model.getUserName());
        map.put("gender", model.getGender());
        map.put("description", model.getDescription());
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl("http://api.dongman.fm/account/updateUserBaseInfo", mapHan, map);
        if (jsonObject == null) {
            loadSourceCallback.onDataLoadFiled();
            return;
        }
        loadSourceCallback.onDataLoaded("");
    }

    @Override
    public void submitUserPicture(Bitmap bitmap, LoadSourceCallback<? super String> loadSourceCallback) {

    }

    @Override
    public void getData(LoadSourceCallback<? super IModel.IUserModel> loadSourceCallback) {
        HashMap<String, String> headerMap = new HashMap<>();
        HashMap<String, String> map = new HashMap<>();
        headerMap.put("x-token", StaticUrl.TOKEN_KEY);
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl("http://api.dongman.fm/account/getBaseInfoByToken", headerMap, map);
        if (jsonObject == null) {
            loadSourceCallback.onDataLoadFiled();
            return;
        }
        loadSourceCallback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downUser(jsonObject));
    }

    @Override
    public void saveData(IModel.IUserModel userModel) {

    }

    @Override
    public void refreshData() {

    }

    @Override
    public void offLine() {
    }
}
